package  summitsoft.android.hiwifi.base;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;

import summitsoft.android.hiwifi.HiWifi;
import summitsoft.android.hiwifi.base.network.HttpUtils;
import summitsoft.android.hiwifi.base.network.NetworkUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.util.Log;



public class WifiConst{
	public static class HttpResult {
		public byte[] data;
		public byte[] contentType;
	}
	
	public static final int WIFI_SCAN_INTERVAL = 3;
	
	public static final int LOCATION_UPDATE_INTERVAL_MILLIS = 1000;
	
	public static final int BUTTON_HEIGHT = 50;
	public static final int BUTTON_WIDTH = 50;
	public static final int BUTTON_MARGIN = 10;
	
	public static final int RADAR_HEIGHT = 320;
	public static final int RADAR_WIDTH = 320;
	
	private static final	int		VIEW_ID_START					= 0x100;
	public static final int VIEW_RADAR_ID = VIEW_ID_START + 1;
	public static final int VIEW_BTN_REFRESH_ID = VIEW_ID_START+2;
	public static final int VIEW_BTN_LIST_ID = VIEW_ID_START+3;
	public static final int VIEW_BTN_DISCONNECT_ID = VIEW_ID_START+4;
	
	/**
	 * Format a float to a field width of 5, with 3
	 * decimals.  MUCH faster than String.format.
	 */
	public static String format53(float val) {
		int before = (int) val;
		int after = (int) ((val - before) * 1000);
		
		String b = "" + before;
		String a = "" + after;
		StringBuilder res = new StringBuilder(" .000");
		int bs = 1 - b.length();
		res.replace((bs < 0 ? 0 : bs), 1, b);
		res.replace(5 - a.length(), 5, a);
		return res.toString();
	}
	
	public static String getLocalWifiIp(int intaddr){
		byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff),
				(byte) (intaddr >> 8 & 0xff), (byte) (intaddr >> 16 & 0xff),
				(byte) (intaddr >> 24 & 0xff) };

		InetAddress addr = null;
		try {
			addr = InetAddress.getByAddress(byteaddr);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		String ip = addr.toString().substring(1);
		return ip;
	}
	
	private static void ensureRouteToHost(String url, boolean isProxySet,
			String proxyAddress) throws IOException {
		ConnectivityManager connMgr = (ConnectivityManager) HiWifi.getContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		int inetAddr;
		if (isProxySet) {
			inetAddr = NetworkUtils.lookupHost(proxyAddress);
			if (inetAddr == -1) {
				throw new IOException("Cannot establish route for " + url
						+ ": Unknown host");
			} else {
				if (!connMgr.requestRouteToHost(
						ConnectivityManager.TYPE_MOBILE, inetAddr)) {
					throw new IOException("Cannot establish route to proxy "
							+ inetAddr);
				}
			}
		} else {
			Uri uri = Uri.parse(url);
			inetAddr = NetworkUtils.lookupHost(uri.getHost());
			if (inetAddr == -1) {
				throw new IOException("Cannot establish route for " + url
						+ ": Unknown host");
			} else {
				if (!connMgr.requestRouteToHost(
						ConnectivityManager.TYPE_MOBILE, inetAddr)) {
					throw new IOException("Cannot establish route to "
							+ inetAddr + " for " + url);
				}
			}
		}
	}
	
	public static HttpResult ArcHttpConnection(int iMethod, byte[] pdu) {
		String serviceCenter = "https://api.skyhookwireless.com/wps2/location";
		String proxyAddress = null;

		try {
			ensureRouteToHost(serviceCenter, false, proxyAddress);
			if (iMethod == HttpUtils.HTTP_GET_METHOD) {
				String uri = new String(pdu);
				Log.v("ArcMM1lib", "http get uri = " + uri);
				return HttpUtils.httpConnection(HiWifi.getContext(), -1, uri,
						null, iMethod, false, proxyAddress, 80);
			} else {

				return HttpUtils.httpConnection(HiWifi.getContext(), -1,
						serviceCenter, pdu, iMethod, false, proxyAddress, 80);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
	

}